import { __PATH } from '../mz/mz.js';
import {
    Read_player,
    existplayer,
    Write_player,
    Write_thing
} from '../mz/mz.js';

export class cundang extends plugin {
    constructor() {
        super({
            /** 功能名称 */
            name: 'cundang',
            /** 功能描述 */
            dsc: '交易模块',
            event: 'message',
            /** 优先级，数字越小等级越高 */
            priority: 600,
            rule: [
                {
                    reg: '^(#|/)今日长度',
                    fnc: 'todaylong',
                },
            ],
        });
    }

    async todaylong(e) {
        // 不开放私聊功能
        if (!e.isGroup) {
            return;
        }

        const usr_qq = e.user_id;

        // 判断是否为匿名创建存档
        const ifexistplay = await existplayer(usr_qq);
        if (ifexistplay) {
            return;
        }

        let player = Read_player(usr_qq);

        // 获取当前日期
        const currentDate = new Date().toLocaleDateString();

        if (player.lastTriggerDate === currentDate) {
            // 如果上次触发日期和当前日期相同，说明今天已经触发过
            e.reply(`您今天已经触发过了，长度为 ${player.坤坤长度}`);
            return;
        }

        // 生成随机数在 -100 到 100 之间（包括边界）
        const randomNum = randomInt(-100, 100);

        let replyMsg = '';
        if (randomNum < 0) {
            replyMsg = `长度为 ${randomNum},今天当个女生吧`;
        } else if (randomNum > 0) {
            replyMsg = `今天的长度为 ${randomNum}`;
        } else {
            replyMsg = `长度为 ${randomNum},今天当个人妖吧吧`;
        }

        player.lastTriggerDate = currentDate;
        player.坤坤长度 = randomNum;

        await Write_player(usr_qq, player);
        e.reply(replyMsg);
    }
}

function randomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
